#include "ListCtrlEx.h"
#include "ContentDataView.h"

BEGIN_EVENT_TABLE(CListCtrlEx, wxListCtrl)
	EVT_CONTEXT_MENU(CListCtrlEx::OnContextMenu)
	EVT_MENU(wxID_NEW, CListCtrlEx::OnMenuNew)
	EVT_MENU(wxID_COPY, CListCtrlEx::OnMenuCopy)
	EVT_MENU(wxID_PASTE, CListCtrlEx::OnMenuPaste)
	EVT_MENU(wxID_DELETE, CListCtrlEx::OnMenuDel)
	EVT_MENU(wxID_SELECTALL, CListCtrlEx::OnMenuSelAll)
	EVT_MENU(wxID_SAVE, CListCtrlEx::OnMenuSave)
END_EVENT_TABLE()

CListCtrlEx::CListCtrlEx(wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, long style) 
	: wxListCtrl(parent, id, pos, size, style)
{
}


CListCtrlEx::~CListCtrlEx()
{

}

void CListCtrlEx::SetDataView(CContentDataView* view)
{
	m_pDataView = view;
}

void CListCtrlEx::OnContextMenu(wxContextMenuEvent& event)
{
	if (GetEditControl() == NULL)
	{
		wxPoint point = event.GetPosition();
		if ( (point.x == -1) && (point.y == -1) )
		{
			wxSize size = GetSize();
			point.x = size.x / 2;
			point.y = size.y / 2;
		}
		else
		{
			point = ScreenToClient(point);
		}
		ShowContextMenu(point);
	}
	else
	{
		event.Skip();
	}
}

void CListCtrlEx::ShowContextMenu(const wxPoint& pos)
{
	int nSel = GetSelectedItemCount();
	
	wxMenu menu;
	menu.Append(wxID_NEW, wxT("N&ew"));
	menu.AppendSeparator();
	menu.Append(wxID_COPY, wxT("C&opy"));
	menu.Append(wxID_PASTE, wxT("P&aste"));
	menu.AppendSeparator();
	menu.Append(wxID_DELETE, wxT("D&el"));
	//menu.AppendSeparator();
	//menu.Append(wxID_SELECTALL, wxT("S&electAll"));
	//menu.Append(wxID_SAVE, wxT("S&ave"));

	if(nSel == 0)
	{
		menu.Enable(wxID_COPY,false);
		menu.Enable(wxID_DELETE,false);
	}
	else if(nSel > 0)
	{
	}

	PopupMenu(&menu, pos.x, pos.y);
}

void CListCtrlEx::OnMenuNew(wxCommandEvent& event)
{
	m_pDataView->OnMenu(event.GetId());
}

void CListCtrlEx::OnMenuCopy(wxCommandEvent& (event))
{
	m_pDataView->OnMenu(event.GetId());
}

void CListCtrlEx::OnMenuPaste(wxCommandEvent& (event))
{
	m_pDataView->OnMenu(event.GetId());
}

void CListCtrlEx::OnMenuDel(wxCommandEvent& (event))
{
	m_pDataView->OnMenu(event.GetId());
}

void CListCtrlEx::OnMenuSelAll(wxCommandEvent& (event))
{
	m_pDataView->OnMenu(event.GetId());
}

void CListCtrlEx::OnMenuSave(wxCommandEvent& (event))
{
	m_pDataView->OnMenu(event.GetId());
}
